package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.GroupHouse;
import com.wuming.house.model.House;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.GroupHouseService;
import com.wuming.house.service.SecondhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/4 16:57
 * @Description:
 */
@Controller
@RequestMapping("/web/groupHouse")
public class GroupHouseController extends BaseController{

    @Autowired
    private GroupHouseService groupHouseService;
    @Autowired
    private SecondhouseService secondhouseService;

    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(GroupHouse groupHouse){
        try {
            GroupHouse gh = groupHouseService.getHouseGroupByGroupIdAndHouseId(groupHouse.getGroupId(),groupHouse.getHouseId());
            if (null==gh){
                groupHouseService.saveGroupHouse(groupHouse);
                return new JsonResult(null,"保存新房分组成功",true);
            }else{
                return new JsonResult(null,"保存新房分组失败,该新房存在在该分组",false);
            }
        }catch (Exception e){
            logger.error("保存新房分组失败,失败信息:{}",e.getMessage());
        }
        return new JsonResult(null,"发生业务异常",false);
    }

    @RequestMapping("/toGroupHouses/{groupId}")
    public String toGroupHouses(@PathVariable Long groupId, Model model){
        if (groupId==1){
            model.addAttribute("groupId",groupId);
            return "/house/groupHouse";
        }
        return "/secondhouse/secondGroupHouse";
    }

    @ResponseBody
    @RequestMapping("/delete/{groupId}/{houseId}")
    public JsonResult delete(@PathVariable Long groupId,@PathVariable Long houseId){
        try {
            groupHouseService.deleteHouse(groupId,houseId);
            return new JsonResult(null,"删除分组楼盘成功",true);
        }catch (Exception e){
            logger.error("删除新房失败,失败原因:{}",e.getMessage());
            return new JsonResult(null,"删除分组楼盘失败",false);
        }
    }

    @ResponseBody
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Integer groupId,@RequestParam(defaultValue = "1")int pageIndex,
                             @RequestParam(defaultValue = "10")int pageSize){
        try {
            if (groupId==1){
                List<House> houses = new ArrayList<>();
                List<GroupHouse> groupHouses = groupHouseService.getHouseByGroupId(groupId,pageIndex,pageSize);

                int total = groupHouseService.getTotal(groupId);
                for (GroupHouse groupHouse : groupHouses){
                    groupHouse.getHouse().setSort(groupHouse.getSort());
                    houses.add(groupHouse.getHouse());
                }
                return CommonResult.createSuccessResult(houses,total,"查询分组下的楼盘信息成功");
            }else{
                //二手房分组
                List<GroupHouse> groupHouses = groupHouseService.getGroupHousesByPage(groupId.longValue(),pageIndex,pageSize);
                int total = groupHouseService.getTotalForOnly(groupId.longValue());
                List<Secondhouse> houses = new ArrayList<>();
                for (GroupHouse groupHouse : groupHouses){
                    Secondhouse secondhouse = secondhouseService.getSecondhouseById(groupHouse.getHouseId());
                    secondhouse.setSort(groupHouse.getSort());
                    houses.add(secondhouse);
                }
                return CommonResult.createSuccessResult(houses,total,"查询分组下的二手房信息成功");
            }
        }catch (Exception e){
            logger.error("查询分组下的楼盘信息失败,失败信息:{}",e.getMessage());
            return CommonResult.createFailureResult("查询分组下的楼盘信息失败");
        }
    }

    @RequestMapping("/toEdit/{groupId}/{houseId}")
    public String toEdit(@PathVariable Long groupId,@PathVariable Long houseId,Model model){
        GroupHouse groupHouse = groupHouseService.getByGroupIdAndHouseId(groupId,houseId);
        model.addAttribute("groupHouse",groupHouse);
        return "/goods/editSort";
    }

    @ResponseBody
    @RequestMapping("/edit")
    public JsonResult edit(GroupHouse groupHouse){
        try {
            groupHouseService.modify(groupHouse);
            return new JsonResult(null,"修改排序成功",true);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("修改排序失败,失败信息:{}",e.getMessage());
            return new JsonResult(null,"修改排序失败",false);
        }
    }
}
